package com.xxw.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxw.yygh.enums.PaymentStatusEnum;
import com.xxw.yygh.model.order.OrderInfo;
import com.xxw.yygh.model.order.PaymentInfo;
import com.xxw.yygh.order.dao.PaymentInfoDao;

import com.xxw.yygh.order.service.PaymentInfoService;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 支付信息表(PaymentInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-03-08 17:50:16
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoDao, PaymentInfo> implements PaymentInfoService {


    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {

        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id",orderInfo.getId());
        PaymentInfo paymentInfo1 = baseMapper.selectOne(paymentInfoQueryWrapper);
        if (paymentInfo1 != null) {
            return;
        }

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }
}

